<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2018/6/25
 * Time: 13:54
 */

namespace app\common\model;


use think\Model;

class Address extends Model
{
    //根据用户ID查询地址表，获取该用户的所有地址信息
    public function getAddressById($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }

        $where = array(
            'user_id' => $id,
            'status' => 1,
        );

        return $this->where($where)->select();

    }

    //根据地址表ID获取当前ID的地址信息
    public function getAreaById($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }

        $where = array(
            'id' => $id,
            'status' => 1,
        );

        return $this->where($where)->find();
    }


    //根据ID修改地址信息
    public function editAddressById($id,$data){
        if(!$id && !is_numeric($id)){
             $this->save($data);
             return $this->id;
        }

        return $this->allowField(true)->save($data,['id'=>$id]);
    }

    //根据ID删除地址信息
    public function DelAddressById($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }

        $data = array(
            'status' => -1
        );

        return $this->allowField(true)->save($data,['id'=>$id]);

    }

    //
    public function getAddress($id){
        if(!$id && !is_numeric($id)){
            exception("传递的ID不合法");
        }
        $where = array(
            'user_id' => $id,
            'status' => 1

        );
        return $this->where($where)->select();
    }


}